package coml.jiyun.hp.zuoye.fragment;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import androidx.core.app.NotificationCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import coml.jiyun.hp.zuoye.R;
import coml.jiyun.hp.zuoye.adapter.RlvHomeAdapter;
import coml.jiyun.hp.zuoye.bean.WanBean;
import coml.jiyun.hp.zuoye.model.ImpWanModel;
import coml.jiyun.hp.zuoye.presenter.ImpWanPresenter;
import coml.jiyun.hp.zuoye.view.WanView;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * A simple {@link Fragment} subclass.
 */
public class RlvHomeFragment extends Fragment implements WanView {

    private RecyclerView mRlv;
    private ArrayList<WanBean.DataBean.DatasBean> datasBeans;
    private RlvHomeAdapter rlvHomeAdapter;
    private RecyclerView home_rlv;
    private LinearLayout ll;
    private int positions;
    private List<WanBean.DataBean.DatasBean> datas;

    public RlvHomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_home, container, false);
        initView(inflate);
        initData();
        return inflate;
    }

    private void initData() {
        ImpWanPresenter impWanPresenter = new ImpWanPresenter(new ImpWanModel(), this);
        impWanPresenter.getData();
    }

    private void initView(View inflate) {
        mRlv = inflate.findViewById(R.id.home_rlv);
        ll = inflate.findViewById(R.id.ll);

        datasBeans = new ArrayList<>();
        rlvHomeAdapter = new RlvHomeAdapter(getActivity(), datasBeans);
        mRlv.setAdapter(rlvHomeAdapter);
        mRlv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rlvHomeAdapter.setOnLongClickListener(new RlvHomeAdapter.OnLongClickListener() {
            @Override
            public void OnLongClick(int position) {
                positions = position;
                pop();
            }
        });

        rlvHomeAdapter.setItemOnClick(new RlvHomeAdapter.itemOnClick() {
            @Override
            public void dj(int i) {
                tongzhi();
            }
        });
    }

    private void tongzhi() {
        NotificationManager manager = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
        //2 创建通知
        Notification build = new NotificationCompat.Builder(getActivity())
                 .setContentText("贾小强")
                 .setContentTitle("标题")
                .setAutoCancel(true)   // 点击后通知消失  (跳转一个页面)
                .setSmallIcon(R.mipmap.ic_launcher).build();

        // 3 发送 (管理器发送通知 设置一个id)
        manager.notify(1,build);

    }

    private void pop() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_pop, null);
        PopupWindow popupWindow = new PopupWindow(view, GridView.LayoutParams.MATCH_PARENT, GridView.LayoutParams.WRAP_CONTENT);
        TextView viewById = view.findViewById(R.id.tv_pop);
        WanBean.DataBean.DatasBean datasBean = datas.get(positions);
        viewById.setText(datasBean.getTitle());
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAtLocation(ll, Gravity.CENTER, 0, 0);
    }

    @Override
    public void onSuccess(WanBean wanBean) {
        datas = wanBean.getData().getDatas();
        datasBeans.addAll(wanBean.getData().getDatas());
        rlvHomeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(String str) {

    }
}